package me.jiatao.taobaotools.activity;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.io.File;

import me.jiatao.taobaotools.R;
import me.jiatao.taobaotools.shot.ScreenShotActivity;
import me.jiatao.taobaotools.utils.ShowMessageUtils;

public class EditActivity extends AppCompatActivity {
    private String imagePath;
    private ImageView taobaobg;
    private long lastClickTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        taobaobg = (ImageView) findViewById(R.id.taobaobg);
        try {
            initTaoBaobgClick();
        } catch (Exception ex) {
            ShowMessageUtils.show(this, ex.getLocalizedMessage());
            ex.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        hideBottomUIMenu();
    }

    private void initTaoBaobgClick() {
        //显示图片
        if (getIntent().hasExtra("IMG_PATH")) {
            imagePath = getIntent().getStringExtra("IMG_PATH");
            //本地文件
            File file = new File(imagePath);
            //加载图片
            Glide.with(this).load(file).into(taobaobg);
        }

        taobaobg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFastClick()) {
                    finish();
                }
            }
        });
        taobaobg.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                screenShot();
                return true;
            }
        });

    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }

    /**
     * the button is fastClick?
     */
    protected boolean isFastClick() {
        long now = System.currentTimeMillis();
        long timediff = now - lastClickTime;
        lastClickTime = now;
        if (timediff < 500) {
            return true;
        }
        return false;
    }

    public void screenShot() {
//        ScreenShotUtils screenShotUtils = new ScreenShotUtils(this);
//        screenShotUtils.setScreenShotListener(new ScreenShotUtils.ScreenShotListener() {
//            @Override
//            public void onScreenShotFinish(boolean isSuccess, String picPath) {
//                if (isSuccess) {
//                    ShowMessageUtils.show(EditActivity.this, "截图保存在: " + picPath);
//                    shareImage(picPath);
//                } else {
//                    ShowMessageUtils.show(EditActivity.this, "截图保存失败,请手动截图分享");
//                }
//            }
//        });
//
//        screenShotUtils.getScreenShot(R.id.imgContainer);
        startActivityForResult(new Intent(this, ScreenShotActivity.class), ScreenShotActivity.SCREEN_SHOT_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ScreenShotActivity.SCREEN_SHOT_SUCC) {
            String imagePath = data.getStringExtra(ScreenShotActivity.SCREEN_SHOT_PATH);
            shareImage(imagePath);
        } else {
            ShowMessageUtils.show(this, "截图失败，请手动截图分享");
        }
    }

    public void shareImage(String imagePath) {
        File imageFile = new File(imagePath);
        if (imageFile.exists()) {
            Intent imageIntent = new Intent(Intent.ACTION_SEND);
            imageIntent.setType("image/jpeg");
            imageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
            startActivity(Intent.createChooser(imageIntent, "分享"));
        }
    }

    private String getResourcesUri(@DrawableRes int id) {
        Resources resources = getResources();
        String uriPath = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                resources.getResourcePackageName(id) + "/" +
                resources.getResourceTypeName(id) + "/" +
                resources.getResourceEntryName(id);
        Toast.makeText(this, "Uri:" + uriPath, Toast.LENGTH_SHORT).show();
        return uriPath;
    }
}
